package tsm.widget.renderer;

import java.io.IOException;

import com.core.tsm.Component;
import com.core.tsm.Container;
import com.core.tsm.Font;
import com.core.tsm.Image;
import com.core.tsm.Label;
import com.core.tsm.List;
import com.core.tsm.layouts.BorderLayout;
import com.core.tsm.layouts.BoxLayout;
import com.core.tsm.list.ListCellRenderer;
import com.core.tsm.plaf.Style;

public class MagicRendererV2 extends Container implements ListCellRenderer {
	private Label icon1;
	private Label description1;
	private Label description2;
	private Container selected = new Container();

	private Label icon2;
	private Label text1;
	private Label text2;
	private Image imageCh;
	private Image sem;
	private Image usm;
	private Image h1;
	private Image h2;

	private Image imagesSelected;
	private Image[] images;
//	private String[] items;
	private String[] subItems;

	public MagicRendererV2() {
		super();
		try {
			imageCh = Image.createImage("/ch.png");
			sem = Image.createImage("/sem.png");
			usm = Image.createImage("/usm.png");
			h1 = Image.createImage("/h1.png");
			h2 = Image.createImage("/h2.png");
		} catch (IOException ex) {
			ex.printStackTrace();
		}

		icon1 = new Label();
		icon1.getSelectedStyle().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED);
		icon1.getSelectedStyle().setBgImage(sem);
		icon1.getSelectedStyle().setBgTransparency(255);
		icon1.getSelectedStyle().setPadding(Component.RIGHT, 0);
		icon1.getSelectedStyle().setMargin(Component.RIGHT, 0);
		description1 = new Label(imageCh);
//		Font font = Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
//		description1.getSelectedStyle().setFont(font);
		description1.getSelectedStyle().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED);
		description1.getSelectedStyle().setBgImage(h1);
		description1.getSelectedStyle().setBgTransparency(255);
		description1.getSelectedStyle().setPadding(Component.LEFT, 0);
		description1.getSelectedStyle().setMargin(Component.LEFT, 0);
		description1.getSelectedStyle().setPadding(Component.BOTTOM, 0);
		description1.getSelectedStyle().setMargin(Component.BOTTOM, 0);
		description2 = new Label(imageCh);
		description2.getSelectedStyle().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED);
		description2.getSelectedStyle().setBgImage(h2);
		description2.getSelectedStyle().setBgTransparency(255);
		description2.getSelectedStyle().setPadding(Component.TOP, 0);
		description2.getSelectedStyle().setMargin(Component.TOP, 0);
		description2.getSelectedStyle().setPadding(Component.LEFT, 0);
		description2.getSelectedStyle().setMargin(Component.LEFT, 0);
		Container a = new Container(new BoxLayout(BoxLayout.Y_AXIS));
		a.addComponent(description1);
		a.addComponent(description2);
		selected.setLayout(new BorderLayout());
		selected.addComponent(BorderLayout.WEST, icon1);
		selected.addComponent(BorderLayout.CENTER, a);
		icon1.setFocus(true);
		description1.setFocus(true);
		description2.setFocus(true);
		setUIID("ListRenderer");

		setLayout(new BorderLayout());
		icon2 = new Label();
		icon2.getUnselectedStyle().setBgTransparency(0);
		//		icon2.getUnselectedStyle().setPadding(0,0,0,0);
		//		icon2.getUnselectedStyle().setMargin(0,0,0,0);
		//		icon2.getUnselectedStyle().setPadding(Component.BOTTOM, 0);
		//		icon2.getUnselectedStyle().setMargin(Component.BOTTOM, 0);
		icon2.getUnselectedStyle().setPadding(Component.RIGHT, 0);
		icon2.getUnselectedStyle().setMargin(Component.RIGHT, 0);
		text1 = new Label(imageCh);
		text1.getUnselectedStyle().setBgTransparency(0);
		text1.getUnselectedStyle().setPadding(Component.LEFT, 0);
		text1.getUnselectedStyle().setMargin(Component.LEFT, 0);
		text1.getUnselectedStyle().setPadding(Component.BOTTOM, 0);
		text1.getUnselectedStyle().setMargin(Component.BOTTOM, 0);
		text2 = new Label(imageCh);
		text2.getUnselectedStyle().setBgTransparency(0);
		text2.getUnselectedStyle().setPadding(Component.TOP, 0);
		text2.getUnselectedStyle().setMargin(Component.TOP, 0);
		text2.getUnselectedStyle().setPadding(Component.LEFT, 0);
		text2.getUnselectedStyle().setMargin(Component.LEFT, 0);
		Container c = new Container(new BoxLayout(BoxLayout.Y_AXIS));
		c.getUnselectedStyle().setPadding(0, 0, 0, 0);
		c.getUnselectedStyle().setMargin(0, 0, 0, 0);
		c.addComponent(text1);
		c.addComponent(text2);
		addComponent(BorderLayout.WEST, icon2);
		addComponent(BorderLayout.CENTER, c);

		//		getUnselectedStyle().setBgTransparency(255);
		getUnselectedStyle().setBgImage(usm);
		getUnselectedStyle().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED);
		//selected.getStyle().setBgTransparency(0xFF);
	}

	public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
		if (isSelected) {
			if (imagesSelected != null) {
				icon1.setIcon(imagesSelected);
			} else {
				if (images.length == 1) {
					icon1.setIcon(images[0]);
				} else {
					icon1.setIcon(images[index]);
				}
			}
			description1.setText(value.toString());
			if (subItems != null) {
				description2.setText(subItems[index]);
			}
			return selected;
		}
		if (images != null) {
			if (images.length == 1) {
				icon2.setIcon(images[0]);
			} else {
				icon2.setIcon(images[index]);
			}
		}
		text1.setText(value.toString());
		if (subItems != null) {
			text2.setText(subItems[index]);
		}
		//		setFocus(true);
		return this;
	}

	public Component getListFocusComponent(List list) {
		text1.setText("");
		text2.setText("");
		icon2.setIcon(null);
		getUnselectedStyle().setBgTransparency(0);
		return this;
	}

	public void setImages(Image[] images) {
		this.images = images;
	}

	//	public void setItems(String[] items) {
	//		this.items = items;
	//	}

	public void setSubItems(String[] subItems) {
		this.subItems = subItems;
	}

	public Image getImagesSelected() {
		return imagesSelected;
	}

	public void setImagesSelected(Image imagesSelected) {
		this.imagesSelected = imagesSelected;
	}

}
